import express from 'express'
import { sleep } from '../../../util/common.js';
import formatTime  from '../../../util/formatTime.js'

import {addSql, detailSql, getResult, likeSql, listSql}  from '../../../util/operateSql.js'
import { getBookDetail, getInfoByBook } from '../../spider/modules/web/web_spider.js';

var router = express.Router();

router.get('/list',(req, res, next ) => {
  const value = req.query.title
  let sql = '';
  
  if (!value) {
    sql = listSql('spider.story',{...req.query})
  } else {
    sql = likeSql('spider.story',{title:value})
  }
  
  getResult(sql).then((result) => {

    res.json(req.app.locals.success(result))
  }).catch((err) => {
    res.json(req.app.locals.error(err))
    
  });
})

router.get('/detail',(req,res, next) => {
  getInfoByBook({...req.query},(e) => {
    if (e.code == 0) {
      res.json(e)
    }
  })
})

/**
 * 存储
 * url: 小说链接
 */
router.get('/save',async (req,res,next) => {
  const datas = [],url = req.query.url
  let id = null,sql_getbook='';
  // 查询id
  const sql_getid = detailSql('spider.story',{link: url})

  const row = await getResult(sql_getid)
  if (row && row.length > 0) {
    id = row[0].id
  }

  // 文章列表
  getInfoByBook(req.query,(e) => {
    if (e.code == 0 && e.data.listMenus.length > 0) {
      e.data.listMenus.forEach((menu,index) => {
        datas.push({title: menu.title, link: menu.link, catalog_index: index + 1})
      })
      datas.forEach(async (data,index) => {
        // 文章详情
        await sleep(500)
        const result = await getBookDetail(datas,index+1)
        const sql_getbook = addSql('spider.articles',{
            story_id: id,
            catalog_title: data.title || '',
            content: result.data,
            link: data.link,
            catalog_index: data.catalog_index}
          )          
          // 存储
          getResult(sql_getbook).then(sta => {
            
            if (index == datas.length - 1) {
              
              res.json('保存完成')
            }
          }).catch(err => {
            
          })
      })
    }
  })
})

router.get('/allSave',async (req,res,next) => {
  const sql = listSql('spider.story',{pageSize: 600,pageIndex: 1})
  
  getResult(sql).then((result) => {
    const list = result
    list.forEach(async (book,index) => {
      let id = null
      const sql_getid = detailSql('spider.story',{link: book.link})

      const row = await getResult(sql_getid)
      if (row && row.length > 0) {
        id = row[0].id
      }
    
      // 文章列表
      getInfoByBook({url: book.link},async (e) => {
        
        await sleep(10000)

        let datas = []
        if (e.code == 0 && e.data.listMenus.length > 0) {
          e.data.listMenus.forEach((menu,index) => {
            datas.push({title: menu.title, link: menu.link, catalog_index: index + 1})
          })
          datas.forEach(async (data,index) => {
            // 文章详情
            await sleep(500)
            const result = await getBookDetail(datas,index+1)
            const sql_getbook = addSql('spider.articles',{
                story_id: id,
                catalog_title: data.title || '',
                content: result.data,
                link: data.link,
                catalog_index: data.catalog_index}
              )          
              // 存储
              getResult(sql_getbook).then(sta => {
                
                if (index == datas.length - 1) {
                  
                }
              }).catch(err => {
                
              })
          })
        }
      })
    })
    // res.json(req.app.locals.success(result))
  }).catch((err) => {
    
    res.json(req.app.locals.error(err))
  });
})

export default router